package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcDeviceOnlineRecord;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.watch.WatchPageQuery;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.watch.DmcWatchVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceOnlineRecordService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcWatchOldmanService;
import com.csun.cmny.provider.validator.DmcDeviceEditValidator;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-08 12:00
 * @Description: the class dmc device watch controller
 */
@RestController
@RequestMapping(value = "/watch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcWatchMainController extends BaseController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private DmcWatchOldmanService dmcWatchOldmanService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcDeviceOnlineRecordService dmcDeviceOnlineRecordService;

    /**
     * 查询组织孝心手环设备列表
     *
     * @param watchPageQuery   the watch page query
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "查询组织孝心手环设备列表")
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/queryGroupWatchListWithPage", method = RequestMethod.POST)
    public Wrapper queryGroupWatchListWithPage(@RequestBody WatchPageQuery watchPageQuery) {

        watchPageQuery.setGroupId(getLoginAuthDto().getGroupId());
        watchPageQuery.setDeviceType(DmcDeviceTypeEnum.XX_WATCH.getIndex());

        PageInfo pageInfo = dmcWatchOldmanService.queryWatchListWithPage(watchPageQuery);
        for (Object o : pageInfo.getList()) {
            DmcWatchVo dmcWatchVo = (DmcWatchVo) o;
            if (!PubUtils.isNull(dmcWatchVo.getOldmanId())) {
                dmcWatchVo.setBind(true);
                /*OldmanBedRoomBuildingVo obv = (OldmanBedRoomBuildingVo) redisTemplate
                        .opsForValue().get(RedisKeyUtil.getOmcOldmanId(dmcWatchVo.getOldmanId()));
                if (PublicUtil.isNotEmpty(obv)) {
                    dmcWatchVo.setOldmanName(obv.getOldmanName());
                }*/
            }
        }

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 添加孝心手环设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "添加孝心手环设备")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addWatch(@Validated @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();

        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.XX_WATCH.getIndex());

        Integer result = dmcDeviceService.addDevice(dmcDevice, getLoginAuthDto());

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 修改孝心手环设备
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改孝心手环设备")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper editWatch(@Validated(value = {DmcDeviceEditValidator.class})
                                  @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.XX_WATCH.getIndex());

        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据Id删除孝心手环
     *
     * @param id    the id
     *
     * @return  the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "根据Id删除孝心手环")
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteDmcWatchById(@PathVariable Long id) {

        Integer result = dmcDeviceService.deleteDmcDeviceById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "操作失败");
        }
        //删除设备在离线日志
        DmcDeviceOnlineRecord dmcDeviceOnlineRecord = new DmcDeviceOnlineRecord();
        dmcDeviceOnlineRecord.setDeviceId(id);
        int delete = dmcDeviceOnlineRecordService.delete(dmcDeviceOnlineRecord);

       // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

}
